{
 "cells": [
  {
   "cell_type": "raw",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": ""
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "file_name = 'data/crm_sop_ivr.csv'\n",
    "df = pd.read_csv(file_name,sep='\\001')\n",
    "df = df.drop_duplicates()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "import requests\n",
    "def get_intentions_from_orochi(dialogue):\n",
    "    url = 'https://predict-phantom.amh-group.com/sop-intention/v1/models/sop-intention:predict'\n",
    "    body = {\n",
    "        \"dialogue\":dialogue\n",
    "    }\n",
    "    req = requests.post(url,json=body)\n",
    "\n",
    "    intents = []\n",
    "    if req.json()[\"sop_info\"][0][0] >= 0.5:\n",
    "        intents.extend([i[1] for i in req.json()['sop_info'][:2]])\n",
    "\n",
    "    if intents:\n",
    "        return str(intents)\n",
    "    else:\n",
    "        return \"\"\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "import json\n",
    "def get_predict_result(item):\n",
    "    dialogues = json.loads(item[\"extra\"])\n",
    "    output_result = []\n",
    "    predicts = []\n",
    "    for sen in dialogues['text'].split(\"|\"):\n",
    "        if sen.startswith(\"1#\"):\n",
    "            predicts.append(sen.replace(\"1#\",\"客服:\"))\n",
    "            output_result.append(sen.replace(\"1#\",\"客服:\"))\n",
    "        elif sen.startswith(\"2#\"):\n",
    "            predicts.append(sen.replace(\"2#\",\"用户:\"))\n",
    "            output_result.append(sen.replace(\"2#\",\"用户:\"))\n",
    "            if len(predicts) >=4:\n",
    "                intents = get_intentions_from_orochi(\"\\n\".join(predicts))\n",
    "                if intents:\n",
    "                    output_result.append(intents)\n",
    "                    output_result.append(\"\\n\")\n",
    "    return \"\\n\".join(output_result)\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "used_intents = [\"拉跑货纠纷-司机把货拉跑\",\" 消费记录查询\",\"咨询如果司机迟到爽约该如何处理\",\"咨询如果产生货损如何处理\",\"咨询如果放空之后如何处理\",\"咨询技术服务费钱款去向\"]\n",
    "\n",
    "\n",
    "for intent in used_intents:\n",
    "    temp_df = df[df['minimal_biz_name'] == intent]\n",
    "    temp_df = temp_df.sample(frac=1.0)\n",
    "    temp_df = temp_df[:5]\n",
    "    temp_df[\"对话\"] = temp_df.apply(get_predict_result,axis=1)\n",
    "    print(temp_df)\n",
    "    break\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}